package yf.mall.controller.api;

import com.yf.annotation.IgnoreAuth;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.dao.MonthShareDao;
import yf.mall.vo.MonthShareSortVo;
import yf.mall.vo.MonthShareVo;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/month/share")
public class ApiMonthShare {

    @Autowired
    private MonthShareDao shareDao;

    @RequestMapping("/sort")
    @IgnoreAuth
    public R sort(Integer type,Integer team){
        Date date = new Date();
        if(type==1){
           LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
           localDate = localDate.minusMonths(1);
           date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }
        List<MonthShareSortVo> shareVos;
        if(team==null){
            shareVos = shareDao.sortQuantityByTeam(date);
        }else {//计算下面的直接买的
            shareVos = shareDao.sortQuantity(date,team);
        }

        BigDecimal sum = shareDao.sumAmount(date);
        return R.ok(shareVos).putExtra("sum",sum);
    }


}
